package com.java;

public class ThreadGroupAPI {
    static class ThreadSon extends Thread {
        @Override
        public void run() {
            System.out.println("线程(" + Thread.currentThread().getName() + ")开始");

            for (int i = 0; i < 5; i++) {
                System.out.println("线程(" + Thread.currentThread().getName() + ")运行中");
            }

            System.out.println("线程(" + Thread.currentThread().getName() + ")结束");
        }
    }

    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("线程组");

        Thread thread1 = new Thread(threadGroup, new ThreadSon());
        Thread thread2 = new Thread(threadGroup, new ThreadSon());
        thread1.start();
        thread2.start();

        // getName：获取线程组名称
        System.out.println(threadGroup.getName()); // 线程组

        // activeCount：获取线程组内运行中的线程数量
        System.out.println(threadGroup.activeCount()); // 2

        // getMaxPriority：获取线程组内线程的最大优先级
        System.out.println(threadGroup.getMaxPriority()); // 10

        // interrupt：中断线程组内所有线程
        System.out.println("中断前1" + thread1.isInterrupted()); // false
        System.out.println("中断前2" + thread2.isInterrupted()); // false
        threadGroup.interrupt();
        System.out.println("中断后1" + thread1.isInterrupted()); // true
        System.out.println("中断后2" + thread2.isInterrupted()); // true
    }
}
